package com.usian.common.exception;

import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 文件名： ExceptionCatch
 * 创作者：王天顺
 *
 * @version 1.0
 * @Date 2022/6/26 17:59
 * @注释
 */
@ControllerAdvice//控制器增强
@Log4j2
public class ExceptionCatch {
    //捕获Exception此类异常
    @ExceptionHandler(NumberFormatException.class)
    @ResponseBody
    public ResponseResult exception(Exception exception) {
        exception.printStackTrace();
        //记录日志
        log.error("catch exception:{}", exception.getMessage());
        //返回通用异常
        return ResponseResult.errorResult(AppHttpCodeEnum.NUMBER_FORMAT_ERROR);
    }

    //捕获Exception此类异常
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public ResponseResult arithmeticException(ArithmeticException exception) {
        exception.printStackTrace();
        //记录日志
        log.error("catch exception:{}", exception.getMessage());
        //返回通用异常
        return ResponseResult.errorResult(AppHttpCodeEnum.ARITHMETIC_ERROR);
    }
}
